(function (a2dmah5) {
    a2dmah5.addExpandPage(0, function (get) {
        const Common = get('UtilsCommon', 'Common');

        return {
            expandInit() {
                this.saveEntityData2 = this.getProperty('saveEntityData2');
                this.detailsProperty = this.getProperty('Details');
            },
            expandSetEntityData({ entityData }) {
                entityData.Status = 0;
                if (this.editEntityData.ProduceOrderId) entityData.ProduceOrderId = this.editEntityData.ProduceOrderId;
                entityData.SaleRowVersion = entityData.SaleRowVersion;
                let blExists = false;
                const materialProductIdList = [];

                for (let i = 0; i < entityData.Details.length; i++) {
                    entityData.Details[i].SaleOrderId = this.editEntityData.SaleOrderId;
                    const id = entityData.Details[i].MaterialProductId;
                    if (materialProductIdList.includes(id)) { blExists = true; break; }
                    else materialProductIdList.push(id);
                }

                if (blExists) {
                    this.alert('原料商品有重复添加');
                    return false;
                }

                return entityData;
            },
            successCallback({ data }) {
                if (data.ProduceOrderId) this.editEntityData.ProduceOrderId = data.ProduceOrderId;
                this.alertSuccess('保存成功');
            },
            expandDataLoad({ data }) {
                this.editEntityData = data;
                if (!data.ProduceOrderId) this.saveEntityData2.setIsVisible(true);
                else if ((data.Status == 0 || data.Status == 1) && Common.isEquals(data.CreateUser, this.loginUser.UserId, true)) this.saveEntityData2.setIsVisible(true);
            },
            setRowDataProperties({ view }) {
                view.properties.forEach(p => {
                    if (p.name === 'MaterialProductId') p.valueChange = this.materialProductIdChange.bind(this);
                });
            },
            materialProductIdChange(value, selectData, property, view) {
                if (!property.isChanged) return;
                if (selectData) {
                    view.properties.forEach(p => {
                        if (p.name === 'Remark' || p.name === 'Width' || p.name === 'Height' || p.name === 'Spec') {
                            p.data[p.name] = selectData[p.name]
                            p.setValue(p.data[p.name]);
                            if (p.name === 'Spec') {
                                p.data.Price = selectData.OutPrice;
                            }
                        }
                    });
                }
                property.isChanged = false;
            }
        }
    });

})(window.a2dmah5);